<template>
  <div class="page">
    <!-- 表单 -->

    <a-card>
      <com-vxe-table
        :columns="columns"
        :axios="page"
        :fromItem="fromItem"
        :formData="formData"
        :initialize="initialize"
        ref="xTable"
      >
        <template #button>
          <vxe-button
            status="primary"
            @click="handlerExportFile()"
            content="导出数据"
          ></vxe-button>
        </template>
        <template #operate="data">
          <vxe-button
            status="primary"
            @click="handlerEdit(data.row)"
            content="编辑"
          ></vxe-button>
          <vxe-button
            status="danger"
            content="删除"
            @click="handlerDelete(data.row)"
          ></vxe-button>
        </template>
      </com-vxe-table>
    </a-card>
    <vxe-modal
      v-model="modal.visabled"
      :width="modal.width"
      :height="modal.height"
      :title="type"
      :destroy-on-close="true"
      show-footer
      @confirm="handleModalOk"
      @cancel="handleClose"
      @close="handleClose"
    >
      <template #default>
        <EvaluationForm
          :jobid="jobid"
          :ybid="ybid"
          :type="type"
          :model="model"
          :ybkp="ybkp"
          :change="handlerFormChange"
          v-if="ybkp"
        ></EvaluationForm>
      </template>
      <template #footer>
        <vxe-button
          v-if="type !== '个人总结'"
          content="退回上一步"
          @click="handlerBock()"
        ></vxe-button>
        <vxe-button
          status="primary"
          content="保存"
          @click="handlerSave()"
        ></vxe-button>
        <vxe-button
          status="primary"
          content="提交"
          @click="handlerSubmit()"
        ></vxe-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
//流程接口
import {
  flowJobPage,
  queryJobByJobId,
  saveFlow,
  submitFlow,
  returnUpperFlowJob,
  queryYbjcyTjPageList,
  exportRckpYbJbxxData
} from "../api/prepareinspectorm";
import comVxeTable from "@/views/everydayCheck/components/vxe-table/com-vxe-table";
import EvaluationForm from "@/views/everydayCheck/prepareinspectorm/EvaluationForm";
import VXETable from 'vxe-table'
export default {
  components: { comVxeTable, EvaluationForm },
  name: "dengjiPage",
  props: {
    type: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      columns: [
        {
          type: "seq",
          title: "序号",
          width: "50",
        },
        {
          field: "fjcyxm",
          title: "姓名",
          width: "150",
        },
        {
          field: "fgzdw",
          title: "工作单位",
        },
        {
          field: "fjclx",
          title: "审评类型",
          width: "150",
        },
        {
          field: "fjcsj",
          title: "审评时间",
        },
        {
          field: "fpj",
          title: "评级",
          width: "150",
        },

        // {
        //   field: "operate",
        //   title: "操作",
        //   fixed: "right",
        //   width: "200",
        // },
      ],
      queryParams: {},
      config: {},
      data: [],
      allAlign: null,
      formData: {
        fjcyxm: "",
        fhjmc: "",
      },
      fromItem: [
        {
          field: "fjcyxm",
          title: "姓名",
          type: "input",
        },
        {
          field: "fpj",
          title: "评级",
          type: "ych-select-tag",
          dictType: "pddj001",
        },
      ],
      page: queryYbjcyTjPageList,
      initialize: {

        onlyCurrentUser: true,
      },
      // fndszbg 填写 大文本
      form: {},
      model: {},
      modal: {
        visabled: false,
        width: "100vw",
        height: "100vh",
        title: "",
      },
      jobid: "",
      ybid: "",
      nextNode: "",
      currentNode: this.type,
      saveForm: {},
      ybkp: null,
    };
  },
  created() {},
  mounted() {},

  methods: {
    async handlerEdit(row) {
      //console.log(row);
      const res = await queryJobByJobId({ jobid: row.jobid });
      if (res.success) {
        this.model = res.result;
        this.jobid = row.jobid;
        this.ybid = row.ybid;
        this.ybkp = res.result.ybkp;
        //console.log(this.ybkp);
        this.nextNode = res.result?res.result.nextNode:'';
        this.modal.visabled = true;
      } else {
        this.$message.error(res.message);
      }
    },
    handlerDelete(row) {
      //console.log(row);
    },
    handleModalOk() {
      //console.log("確定");
    },
    handleClose() {
      //console.log("取消");
    },
    handleAdd() {
      this.modal.visabled = true;
    },
    handlerSave() {
      const form = this.getQueryPamas();
      saveFlow(form)
        .then((res) => {
          if (res.success) {
            this.$VxeMessage({ content: res.message, status: "success" });
            // this.handlerEdit(form);
          } else {
            this.$VxeMessage({ content: res.message, status: "error" });
          }
        })
        .catch((error) => {
          this.$VxeMessage({ content: "未知异常", status: "error" });
        });
    },
    handlerSubmit() {
      const form = this.getQueryPamas();
      if (this.type.indexOf("个人总结") !== -1) {
        if (!form.map.fsxjcyzj) {
          this.$VxeMessage({
            content: "请填写实习个人总结",
            status: "warning",
          });
          return;
        }
      }
      if (this.type.indexOf('组长评价') !==-1) {
        if (!form.map.fzzyj) {
          this.$VxeMessage({ content: "请填写组长意见", status: "warning" });
          return;
        }
        if (!form.map.fpj) {
          this.$VxeMessage({ content: "请选择评定等级", status: "warning" });
          return;
        }
      }
      if(!form.dbrid) {
        this.$VxeMessage({ content: "请选择待办人", status: "warning" });
          return;
      }
      submitFlow(form)
        .then((res) => {
          //console.log(res);
          if (res.success) {
            this.$VxeMessage({ content: res.message, status: "success" });
            this.modal.visabled = false;
            this.$refs.xTable.getQueryList()
          } else {
            this.$VxeMessage({ content: res.message, status: "error" });
          }
        })
        .catch((error) => {
          this.$VxeMessage({ content: "未知异常", status: "error" });
        });
    },
    getQueryPamas() {
      //console.log(this.saveForm);
      const { dbrid, opinion, smgflag,fsxjcyzj,fzzyj,fzzxm,fpj } = this.saveForm;
      const formData = {};
      formData.map = {fsxjcyzj,fzzxm,fzzyj,fpj};
      formData.jobid = this.jobid;
      formData.ybid = this.ybid;
      formData.nextNode = this.nextNode;
      formData.currentNode = this.currentNode;
      formData.dbrid = dbrid;
      formData.smgflag = smgflag;
      formData.opinion = opinion;
      return formData;
    },
    handlerBock() {
      const form = this.getQueryPamas();
      if (!form.opinion) {
        this.$VxeMessage({ content: "请填写办理意见", status: "warning" });
        return;
      }
      returnUpperFlowJob(form)
        .then((res) => {
          if (res.success) {
            this.$VxeMessage({ content: res.message, status: "success" });
            this.modal.visabled = false;
            this.$refs.xTable.getQueryList()
          } else {
            this.$VxeMessage({ content: res.message, status: "error" });
          }
        })
        .catch((error) => {
          this.$VxeMessage({ content: "未知异常", status: "error" });
        });
    },
    handlerFormChange(form) {
      this.saveForm = {...this.saveForm,...form};
      //console.log(this.saveForm);
    },
    handlerExportFile() {
      exportRckpYbJbxxData().then(res => {
        try {
          VXETable.saveFile({ filename: '预备审评员统计', type: 'xlsx', content: res })
        } catch (error) {
          this.$message.error('未知异常')
        }
      }).catch(error => {
        this.$message.error('未知异常')
      })
    }
  },
};
</script>

<style lang="less" scoped>
// @import "../assets/table.less";
@import url("../assets/table.less");
</style>
